{%- set allOrgPeersDict=dict() -%}
version: 1.0.0
{# **********************peers setting********************************* #}
peers:
{%- for org in orgs %}	
{% set orgPeers=[] %}
{%- for i in  range(0,org.count|int) %}	
{%- set peer = 'peer'+(i|string)+'.'+org.domain %}
{%- set peerDomain = peer+':'+(servers|selectattr('domain','equalto', peer) | map(attribute='port') | list | last) %}
{%- if orgPeers.append(peerDomain) %}{% endif %}
  {{peerDomain}}:
    url: grpc://{{peerDomain}}
    tlsCACerts:
      pem: |
{% set pem = (pems|selectattr('orgName','equalto', org.domain) | map(attribute='pem') | list | last) %}
{% 	for line in pem.split('\n') %}
        {{ line }}
{% 	endfor 		 %}
{%- endfor %}
{%- if allOrgPeersDict.__setitem__(org.name,orgPeers) %}{% endif %}
{%- endfor %}

{# **********************organizations setting********************************* #}
organizations:
{%- for og in orgs %}	
  {{og.mspName}}:
    peers:
{%- for peer in allOrgPeersDict.get(og.name) %}	
     - {{peer}}
{% endfor %}
    mspid: {{org.mspName}}
    cryptoPath: /tmp/msp
{%  if og.name == org.name  %}
    certificateAuthorities: 
	  - {{'tlsca.'+org.domain}}
{%  else         %}	
    certificateAuthorities: []
{%  endif        %}
{%- endfor %}

{# **********************orderers setting********************************* #}
orderers:
{% set ordererArr=[] %}
{%- for i in  range(0,orderer.count|int) %}
{%- set ord = 'orderer'+(i|string)+'.'+orderer.domain %}
{%- set ordererDomain = ord+':'+(servers|selectattr('domain','equalto', ord) | map(attribute='port') | list | last) %}
{%- if ordererArr.append("orderer"+(i|string)) %}{% endif %}
  orderer{{i}}:
    url: grpc://{{ordererDomain}}
    tlsCACerts:
      pem: |
{%  set pem = (pems|selectattr('orgName','equalto', orderer.domain) | map(attribute='pem') | list | last) %}
{% 	for line in pem.split('\n') %}
        {{ line }}
{% 	endfor 		 %}
    grpcOptions:
      ssl-target-name-override: {{ordererDomain}}
{%- endfor %} 

{# **********************org setting********************************* #}
name: Iwhale-Baas-Config
description: The network generated by iwhale BaaS.
client:
  organization: {{org.mspName}}
  logging:
    level: info
  credentialStore:
    path: /tmp/msp/signcerts
    cryptoStore:
      path: /tmp/msp
  BCCSP:
    security:
      default:
        provider: SW
      level: 256
      softVerify: true
      enabled: true
      hashAlgorithm: SHA2

{# **********************channel setting********************************* #}	  
channels:
{%- for channel in channels %}	
  {{channel.name}}:
    peers:
{%-   for org in channel.orgs    %}
{%-      for peer in allOrgPeersDict.get(org)%}  
      {{peer}}:
        chaincodeQuery: true
        endorsingPeer: true
        discover: true
        eventSource: true
        ledgerQuery: true
{%      endfor                              %}
{%   endfor                     %}
    orderers:
{%-  for orderer in ordererArr  %}	
      - {{orderer}}
{%  endfor                     %}
{% endfor %} 	

{# **********************ca setting********************************* #}

certificateAuthorities:
{% set domain='tlsca.'+org.domain %}
  {{domain}}:
{% set url = 'https://'+domain+':'+(servers|selectattr('domain','equalto', domain) | map(attribute='port') | list | last) %}
    url: {{url}}
    tlsCACerts:
      pem: |
{% set pem = (pems|selectattr('orgName','equalto', org.domain) | map(attribute='pem') | list | last) %}
{% 	for line in pem.split('\n') %}
        {{ line }}
{% 	endfor 		 %}
    caName: {{domain}}
